package com.example.myapplication;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.dao.ConfirmDao;
import com.example.myapplication.dao.RegisterDao;
import com.example.myapplication.entity.User;


public class RegisterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        setContentView(R.layout.regeister);

        getWindow().setStatusBarColor(Color.TRANSPARENT);
        ImageButton btngoback = findViewById(R.id.ibtn_back);
        btngoback.setOnClickListener(v -> finish());

        EditText account = findViewById(R.id.et_QQ1);
        EditText passwd = findViewById(R.id.et_password1);
        EditText name = findViewById(R.id.et_name1);
        Button button = findViewById(R.id.btn_register);
        RegisterDao registerDao = new RegisterDao();
        Handler handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                String username = ((User) msg.obj).getName();
                ConfirmDao.islogin = true;
                ConfirmDao.username = username;
                Toast.makeText(getApplicationContext(), "欢迎" + username, Toast.LENGTH_SHORT).show();
                finish();
            }
        };
        button.setOnClickListener(v -> {
            registerDao.register(name.getText().toString(), account.getText().toString(), passwd.getText().toString());
            while (!registerDao.ischanged) {
                System.out.println("520");
            }
            new ConfirmDao().send(account.getText().toString(), passwd.getText().toString(), handler);

        });
    }

}